package com.yinhai.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhai.cfg.AppConfig;
import com.yinhai.model.CloudNote;
import com.yinhai.service.CloudNoteServiceImpl;
import com.yinhai.util.GetCurrentTime;

public class CloudNoteContentActivity extends Activity {
	private ImageView iv_back, iv_save, iv_clear;
	private EditText ev;
	private TextView tv_title;
	Intent getit;
	String type, uuid, pid ="";
	private CloudNoteServiceImpl cnServiceIpml;
	CloudNote editCloudNote, receiveCloudNote, pCloudNote =null;
	private String phone;
	String ev_content = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cloudnotecontent);
		editCloudNote = new CloudNote();
		SharedPreferences preferences = getSharedPreferences(
				AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
		phone = preferences.getString("name", "");
		cnServiceIpml = new CloudNoteServiceImpl(CloudNoteContentActivity.this);
		getit = getIntent();
		if(getit.hasExtra("type")){
		    type = getit.getStringExtra("type");
		}
        if(getit.hasExtra("pid")){
            pid = getit.getStringExtra("pid");     
         }
        if(getit.hasExtra("uuid")){
            uuid = getit.getStringExtra("uuid");
        }
		
		
		

		iv_back = (ImageView) findViewById(R.id.btn_back);
		iv_save = (ImageView) findViewById(R.id.btn_save);
		iv_clear = (ImageView) findViewById(R.id.btn_clear);
		tv_title = (TextView) findViewById(R.id.txt_contact);
		ev = (EditText) findViewById(R.id.note);

		/*
		 * ev.addTextChangedListener(new TextWatcher() {
		 * 
		 * @Override public void onTextChanged(CharSequence s, int start, int
		 * before, int count) { // TODO Auto-generated method stub
		 * 
		 * }
		 * 
		 * @Override public void beforeTextChanged(CharSequence s, int start,
		 * int count, int after) { // TODO Auto-generated method stub ev_content
		 * = s.toString(); }
		 * 
		 * @Override public void afterTextChanged(Editable s) { // TODO
		 * Auto-generated method stub
		 * 
		 * } });
		 */
		iv_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		iv_clear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ev.setText("");
			}
		});

		iv_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (ev.getText().toString().trim().equals("")) {
					Toast.makeText(CloudNoteContentActivity.this, "记事内容不能为空",
							Toast.LENGTH_SHORT).show();
				} else {
					if (type.equals("add")) {
						editCloudNote.setUuid(GetCurrentTime.getUuid());
						editCloudNote.setCreate_time(GetCurrentTime.getTime());
						editCloudNote.setNote(ev.getText().toString());
						editCloudNote.setPid(pid);
						editCloudNote.setPhone(phone);
					}
					new SaveContentAsyncTask().execute();
				}

			}
		});

		initData();

	}

	void initData() {
		new InitContentAsyncTask().execute();
	}

	private class SaveContentAsyncTask extends AsyncTask<Void, Void, Integer> {
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			if (type.equals("add")) {
				Toast.makeText(CloudNoteContentActivity.this, "添加成功",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(CloudNoteContentActivity.this, "编辑成功",
						Toast.LENGTH_SHORT).show();
			}

			Intent imgroupBroadcast = new Intent("com.cloudnote.update");
			imgroupBroadcast.putExtra("bc_uuid", pid);
			imgroupBroadcast.putExtra("is_del", "0");
			CloudNoteContentActivity.this.sendBroadcast(imgroupBroadcast);
			CloudNoteContentActivity.this.finish();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (type.equals("add")) {
				cnServiceIpml.insert(editCloudNote);
			} else {
				cnServiceIpml.update(uuid, ev.getText().toString(),
						GetCurrentTime.getTime());
			}
			return 0;
		}
	}

	private class InitContentAsyncTask extends AsyncTask<Void, Void, Integer> {
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			if (type.equals("add")) {
				ev.setText("");
			} else {
			    if(receiveCloudNote !=null){
			        ev.setText(receiveCloudNote.getNote());
			    }
				
			}
			if(pCloudNote !=null){
			    tv_title.setText(pCloudNote.getNote());
			}
			
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			// TODO Auto-generated method stub
			pCloudNote = cnServiceIpml.getCloudNoteByUuid(uuid);
			if (!type.equals("add")) {
				receiveCloudNote = cnServiceIpml.getCloudNoteByUuid(uuid);
			}

			return 0;
		}
	}
}
